//
//  UIStackView_Extension.swift
//  swiftProductTemplate
//
//  Created by 刘圣洁 on 2025/6/20.
//

import UIKit

extension UIStackView {
    /// 快速创建水平栈视图
    /// - Parameters:
    ///   - arrangedSubviews: 子视图数组
    ///   - spacing: 间距
    ///   - alignment: 对齐方式
    ///   - distribution: 分布方式
    static func horizontalStack(
        arrangedSubviews: [UIView] = [],
        spacing: CGFloat = 0,
        alignment: Alignment = .fill,
        distribution: Distribution = .fill
    ) -> UIStackView {
        let stackView = UIStackView(arrangedSubviews: arrangedSubviews)
        stackView.axis = .horizontal
        stackView.spacing = spacing
        stackView.alignment = alignment
        stackView.distribution = distribution
        return stackView
    }
    
    /// 快速创建垂直栈视图
    /// - Parameters:
    ///   - arrangedSubviews: 子视图数组
    ///   - spacing: 间距
    ///   - alignment: 对齐方式
    ///   - distribution: 分布方式
    static func verticalStack(
        arrangedSubviews: [UIView] = [],
        spacing: CGFloat = 0,
        alignment: Alignment = .fill,
        distribution: Distribution = .fill
    ) -> UIStackView {
        let stackView = UIStackView(arrangedSubviews: arrangedSubviews)
        stackView.axis = .vertical
        stackView.spacing = spacing
        stackView.alignment = alignment
        stackView.distribution = distribution
        return stackView
    }
    
    /// 添加多个子视图
    /// - Parameter views: 子视图数组
    func addArrangedSubviews(_ views: [UIView]) {
        views.forEach { addArrangedSubview($0) }
    }
    
    /// 移除所有子视图
    func removeAllArrangedSubviews() {
        arrangedSubviews.forEach {
            removeArrangedSubview($0)
            $0.removeFromSuperview()
        }
    }
    
    /// 设置边距
    /// - Parameters:
    ///   - top: 上边距
    ///   - leading: 左边距
    ///   - bottom: 下边距
    ///   - trailing: 右边距
    func setCustomSpacing(
        top: CGFloat = 0,
        leading: CGFloat = 0,
        bottom: CGFloat = 0,
        trailing: CGFloat = 0
    ) {
        isLayoutMarginsRelativeArrangement = true
        layoutMargins = UIEdgeInsets(top: top, left: leading, bottom: bottom, right: trailing)
    }
}
